{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "qwen-plus\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/envs/chatchat/lib/python3.8/site-packages/langchain_core/_api/deprecation.py:119: LangChainDeprecationWarning: The class `LLMChain` was deprecated in LangChain 0.1.17 and will be removed in 0.3.0. Use RunnableSequence, e.g., `prompt | llm` instead.\n",
      "  warn_deprecated(\n",
      "/root/miniconda3/envs/chatchat/lib/python3.8/site-packages/langchain_core/_api/deprecation.py:119: LangChainDeprecationWarning: The method `Chain.run` was deprecated in langchain 0.1.0 and will be removed in 0.2.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "为一家生产彩色袜子的公司起名时，可以考虑将色彩、创意、时尚和趣味性等元素融入其中。以下是一些建议的名字，希望能激发您的灵感：\n",
      "\n",
      "1. **彩虹步履** - 彩虹代表着多样性和无限可能，而“步履”则暗示着穿着这些袜子行走时的美好体验。\n",
      "2. **绚彩袜语** - “绚彩”强调了产品的多彩特性，“袜语”则带有一种轻松愉快的感觉，仿佛每双袜子都有它自己的故事要说。\n",
      "3. **色彩之旅** - 这个名字传达了一种探索未知、享受生活乐趣的品牌形象。\n",
      "4. **袜趣横生** - 结合了“袜子”与“乐趣”，适合追求个性和创意的品牌定位。\n",
      "5. **缤纷脚印** - “缤纷”突出了产品颜色丰富多样的特点，“脚印”则象征着每一步都留下美好记忆。\n",
      "6. **趣色无界** - 强调了品牌对于色彩运用没有界限的态度，鼓励消费者大胆尝试不同的搭配。\n",
      "7. **袜艺工坊** - 适用于注重设计感和手工艺品质的品牌，展现出对细节的关注和对美的追求。\n",
      "\n",
      "选择公司名称时，请确保进行适当的市场调研，以确认所选名称未被其他企业注册使用，并且符合目标市场的文化和语言习惯。希望这些建议能够帮助到您！\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "from langchain_community.llms import Tongyi\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.chains import LLMChain\n",
    "\n",
    "# 加载 .env 文件\n",
    "load_dotenv()  # 默认会加载位于项目根目录的 .env 文件\n",
    "\n",
    "# 创建语言模型对象\n",
    "llm = Tongyi(dashscope_api_key=os.getenv(\"DASHSCOPE_API_KEY\"), model_name=\"qwen-plus\")\n",
    "print(llm.model_name)\n",
    "# 创建提示模板\n",
    "prompt = PromptTemplate(\n",
    "    input_variables=[\"product\"],\n",
    "    template=\"一个生产{product}的公司，建议的好名字是什么?\",\n",
    ")\n",
    "\n",
    "# 创建 LLMChain 对象\n",
    "chain = LLMChain(llm=llm, prompt=prompt)\n",
    "\n",
    "# 运行链式调用\n",
    "print(chain.run(\"彩色袜子\"))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "为您的智能手机生产公司起名时，可以考虑将公司的核心价值观、产品特点或目标市场等因素融入其中。这里为您提供几个建议，希望能够激发您的灵感：\n",
      "\n",
      "1. 智汇科技（Zhihui Technology）：寓意智慧汇聚，技术创新。\n",
      "2. 明日通讯（Mingri Communications）：代表对未来通信技术的追求与探索。\n",
      "3. 极速生活（Jisu Life）：强调产品带来的高效便捷生活方式。\n",
      "4. 无限视界（Wuxian Vision）：象征着开放视野，连接世界每一个角落。\n",
      "5. 慧眼识真（Huiyan Shizhen）：突出智能设备能够帮助用户更好地理解周围环境。\n",
      "\n",
      "请根据您企业的具体定位和文化背景选择合适的名字，或者以此为基础进行创造性的发挥。\n"
     ]
    }
   ],
   "source": [
    "from langchain_community.chat_models.tongyi import ChatTongyi\n",
    "from langchain.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.chains import LLMChain\n",
    "\n",
    "# 创建人类消息提示模板\n",
    "human_message_prompt = HumanMessagePromptTemplate(\n",
    "    prompt=PromptTemplate(\n",
    "        template=\"一个生产{product}的公司，建议的中文公司名称是什么?\",\n",
    "        input_variables=[\"product\"],\n",
    "    )\n",
    ")\n",
    "\n",
    "# 创建聊天提示模板\n",
    "chat_prompt_template = ChatPromptTemplate.from_messages([human_message_prompt])\n",
    "\n",
    "# 创建聊天模型对象\n",
    "# 初始化聊天模型\n",
    "chat = ChatTongyi(\n",
    "    model='qwen-plus',\n",
    "    temperature=0,\n",
    "    api_key=os.getenv(\"DASHSCOPE_API_KEY\")\n",
    ")\n",
    "\n",
    "# 创建 LLMChain 对象\n",
    "chain = LLMChain(llm=chat, prompt=chat_prompt_template)\n",
    "\n",
    "# 运行链式调用\n",
    "print(chain.run(\"智能手机\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
      "\n",
      "Current conversation:\n",
      "\n",
      "Human: LangChain是什么？\n",
      "AI:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'LangChain是一个开源框架，用于构建和部署基于语言模型的应用程序。它提供了一套工具和库，使开发者能够更容易地将自然语言处理（NLP）技术集成到他们的项目中。LangChain支持多种流行的预训练模型，如GPT-3、BERT等，并提供了丰富的API和文档来帮助开发者快速上手。\\n\\n具体来说，LangChain有以下几个主要特点：\\n\\n1. **模块化设计**：开发者可以根据需要选择不同的组件，灵活构建自己的应用。\\n2. **多模型支持**：支持多种预训练模型，方便开发者根据任务需求选择合适的模型。\\n3. **易于集成**：提供了与现有系统的集成工具，使得NLP功能可以无缝融入现有的应用程序中。\\n4. **社区活跃**：有一个活跃的开发者社区，提供了大量的示例代码和教程，帮助新手快速入门。\\n\\n如果你对某个特定方面感兴趣，比如如何在项目中使用LangChain，或者有哪些具体的用例，我可以提供更详细的信息。'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.chains import ConversationChain\n",
    "from langchain_community.chat_models.tongyi import ChatTongyi\n",
    "\n",
    "# 创建聊天模型对象\n",
    "chat = ChatTongyi(\n",
    "    model='qwen-plus',\n",
    "    temperature=0,\n",
    "    api_key=os.getenv(\"DASHSCOPE_API_KEY\")\n",
    ")\n",
    "# 创建 ConversationChain 对象并启用调试信息\n",
    "conversation = ConversationChain(\n",
    "    llm=chat,\n",
    "    verbose=True\n",
    ")\n",
    "\n",
    "# 运行对话\n",
    "conversation.run(\"LangChain是什么？\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chatchat",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
